package domain;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Formula;

import domain.Confirmation.Calification;
import domain.Confirmation.Confirmated;
import domain.exceptions.RequiredFieldsException;
import domain.exceptions.RestrictionBuyingException;

@Entity
@Table(name = "compras")
public class Purchase extends PersistentEntity{
	
	@ManyToOne
	@JoinColumn(name = "usuario")
	private User user;
	
	@ManyToOne
	@JoinColumn(name = "articulo")
	private Item item;
	private String comment;
	
	@Column(nullable = false)
	private int qty;
	@Column(nullable = false)
	private double price;
	@Column(nullable = false)
	private Date purchaseDate;
	
	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name = "confirmacion")
	private Confirmation confirmation;
	
	//Indica morosidad de la compra
	private boolean debtPending;
	
	@Formula(value = "price * qty * 0.1")
	private double commission;
	
	//Hibernate
	public Purchase() {
	}
	
	public Purchase(User user, Item item, String comment, int count, double price, Date purchaseDate) throws RestrictionBuyingException{
		if(user==null || item==null || purchaseDate==null)
			throw new RequiredFieldsException();
		if(count<=0 || price<=0)
			throw new IllegalArgumentException();
		if(user.getIsBlock() || item.getUser().getIsBlock()){
			throw new RestrictionBuyingException(RestrictionBuyingException.BLOCK_CODE);
		}
		if(user.equals(item.getUser())){
			throw new RestrictionBuyingException(RestrictionBuyingException.SAME_USER_CODE);
		}
		if(item.getCount()<count){ //valido cantidades aca tb
			throw new RestrictionBuyingException(RestrictionBuyingException.STOCK_RESTRICTION_CODE);
		}
		
		if(item.getSuspended()){
			throw new IllegalArgumentException();
		}
		
		this.user = user;
		this.item = item;
		this.comment = comment;
		this.qty = count;
		this.price = price;
		this.purchaseDate = purchaseDate;
		this.confirmation = new Confirmation(); 
		this.debtPending = false;
		//TODO: esto tal vez no hace falta porque con el @formula ya cuando carga
		//la compra, lo asigna
		this.commission = item.getPrecio() * count * 0.1;
	}
	//TODO: para que esta este metodo? si en los controllers se esta pidiendo el confirmation y seteandole
	// las cosas desde ahi...
	public void setSellerConfirmation(String comment, 
			Calification calification, Confirmated sellerConfirmation, Confirmated buyerConfirmation){
		confirmation.setSellerConfirmation(sellerConfirmation);
		confirmation.setCalification(calification);
		confirmation.setComment(comment);
		// Se confirmo la compra... ahora es morosa
		if (confirmation.succesful()) {
			debtPending = true;
		}
	}
	
	//TODO: idem
	public void setBuyerConfirmation(Confirmated buyerConfirmation){
		confirmation.setBuyerConfirmation(buyerConfirmation);
		//Se confirmo la compra... ahora es morosa
		if (confirmation.succesful()) {
			debtPending = true;
		}
	}
	
	public void cancelPurchase(){
		confirmation.setBuyerConfirmation(Confirmated.dontrealize);
		confirmation.setSellerConfirmation(Confirmated.dontrealize);
	}
	
	public String toString(){
		return this.user.getName() + " " + this.item.getName() + " " + this.qty;
	}
	public User getUser() {
		return user;
	}

	public Item getItem() {
		return item;
	}

	public String getComment() {
		return comment;
	}

	public int getQty() {
		return qty;
	}

	public double getPrice() {
		return price;
	}

	public Date getPurchaseDate() {
		return purchaseDate;
	}
	
	public Confirmation getConfirmation(){
		return confirmation;
	}
	
	public boolean isDebtPending() {
		return debtPending;
	}
	
	public void setDebtPending(boolean debtPending) {
		this.debtPending = debtPending;
	}

	public double calcCommission() {
		return price * qty * 0.1;
	}

	public double getCommission() {
		return commission;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + qty;
		result = prime * result + ((item == null) ? 0 : item.hashCode());
		long temp;
		temp = Double.doubleToLongBits(price);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result
				+ ((purchaseDate == null) ? 0 : purchaseDate.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Purchase other = (Purchase) obj;
		if (qty != other.qty)
			return false;
		if (item == null) {
			if (other.item != null)
				return false;
		} else if (!item.equals(other.item))
			return false;
		if (Double.doubleToLongBits(price) != Double
				.doubleToLongBits(other.price))
			return false;
		if (purchaseDate == null) {
			if (other.purchaseDate != null)
				return false;
		} else if (!purchaseDate.equals(other.purchaseDate))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}
	
	
}
